//
//  NewFeatureViewController.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/7.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
/// 可重用标示符
  private let NewFeatureCellID = "NewFeatureCell"
//  图片数量
    private let NewFeatureCount = 4
//    MARK: - 构造函数
    init(){
//        注意 UICollectioniewLayout 是抽象性的
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
//注册 cell
        collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: NewFeatureCellID)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

// MARK: - 数据源方法

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureCellID, forIndexPath: indexPath) as!NewFeatureViewCell
        cell.backgroundColor = UIColor.redColor()
        cell.imageIndex = indexPath.item
        return cell
    }
//    MARK:代理方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let offset = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        if offset != NewFeatureCount - 1  {
            return
        }
       (collectionView?.visibleCells().last as? NewFeatureViewCell)?.showStartButton()
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

private class NewFeatureViewCell:UICollectionViewCell{
    var imageIndex:Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
//    MARK: - 懒加载控件
    lazy var iconView = UIImageView()
    lazy var startButton :UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - 设置界面
    private func setUI(){
//    1 添加控件
        addSubview(iconView)
        addSubview(startButton)
//    2  设置控件属性
        startButton.hidden = true
        iconView.frame = bounds
        startButton.layer.cornerRadius = 5
        startButton.layer.masksToBounds = true
//    3 自动布局
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
        }
//    4 添加开始按钮监听方法
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
    }
//   监听方法
    @objc private func clickStartButton(){
        NSNotificationCenter.defaultCenter().postNotificationName("TcgSwitchRootViewControllerNotification", object: nil)
    }
    
//    MARK: - 动画显示开始按钮
    private func showStartButton(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.2,//动画时长
            delay: 0,//延迟时间
            usingSpringWithDamping: 0.8,//弹力系数
            initialSpringVelocity: 10,//初始速度
            options: [],
            animations: { () -> Void in
               self.startButton.transform = CGAffineTransformIdentity
            }) { (_) in
                self.startButton.userInteractionEnabled =  true
                
        }
    }
}
